//----------------------------------*-C++-*----------------------------------//
/*!
 * \file   testSpherical_Harmonics.cc
 * \author Jeremy Roberts
 * \date   Jul 1, 2011
 * \brief  Unit test of Spherical_Harmonics class
 * \note   Copyright (C) 2011 Jeremy Roberts. 
 */
//---------------------------------------------------------------------------//
// $Rev::                                               $:Rev of last commit
// $Author::                                            $:Author of last commit
// $Date::                                              $:Date of last commit
//---------------------------------------------------------------------------//

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>

#include "angle/Spherical_Harmonics.hh"
#include "../utilities/DBC.hh"
#include "../utilities/Soft_Equivalence.hh"

using namespace slabtran;
using namespace std;
using util::soft_equiv;

extern double input[10][6];
extern double ref[10][36];

// test driver
int main(int argc, char *argv[])
{
	Require(argc==2);
	int test = atoi(argv[1]);
	cout << " --- testSpherical_Harmonics --- " << endl;
    cout << " ---- test number " << test;

	int test_value = 1; // 0 for pass, 1 for fail
	int L = 3;          // legendre order

	// column index for the reference data
	int idx = 0;
	if ( test == 2 )
		idx = 16;
	else if ( test == 3 )
	    idx = 32;

	double Y     = 0.0;
	double mu    = 0.0;
	double eta   = 0.0;
	double xi    = 0.0;
	double phi   = 0.0;

	int mm = 0; // bound on order to test 1D, Pn computations
    for ( int l = 0; l < L; l++ )
    {
    	if ( test < 3 )
    		mm = l;
    	else
    		mm = 0;
    	for ( int m = -mm; m <= mm; m++ )
    	{
    		for ( int i = 0; i < 10; i++ )
    		{
    			if ( test == 1 ) // test of triplet
    			{
    				mu  = input[i][0];
    				eta = input[i][1];
    				xi  = input[i][2];
    				Y = Spherical_Harmonics::Y_lm( l, m, mu, eta, xi );
    				if ( !soft_equiv( Y, ref[i][idx] ) )
    				{
    					cout << " failed at l=" << l << " m=" << m
    					     << " mu=" << mu << " eta=" << eta
    					     << " xi=" << xi << " i=" << i
    					     << " idx=" << idx << endl;
    					return 1;
    				}
    			}
    			else if ( test == 2 )
    			{
    				xi    = input[i][3];
    				phi   = input[i][4];
    				Y = Spherical_Harmonics::Y_lm( l, m, xi, phi );
    				if ( !soft_equiv( Y, ref[i][idx] ) )
    				{
    					cout << " failed at l=" << l << " m=" << m
    					     << " xi=" << xi << " phi=" << phi
    					     << " i=" << i
    					     << " idx=" << idx << endl;
    					return 1;
    				}
    			}
    			else if  ( test == 3 )
    			{
    				xi    = input[i][5];
    				Y = Spherical_Harmonics::Y_lm( l, xi );
    				if ( !soft_equiv( Y, ref[i][idx] ) )
    				{
    					cout << " failed at l=" << l
    					     << " xi=" << xi
    					     << " i=" << i
    					     << " idx=" << idx << endl;
    					return 1;
    				}
    			}
    			else
    			{
    				cout << " test " << test << " not available " << endl;
    				return 1;
    			}
    		}
    		idx++;
    	}
    }
    cout << " passed. ---- " << endl;
	return 0;
}

//  (mu1,eta1,xi1), (xi2,phi2), (xi3)
//  randomly generated directions
double input[10][6] =
{       {  0.119134946215745 , -0.475473748388221 , -0.871625825216203 , 0.994006543213295 , 2.327057390239852 , -0.824999300846829  } ,
		{  0.506392746855438 , -0.676539811544032 , 0.534659021553149 , -0.551657002033746 , 5.287677976689000 , 0.280233096493430  } ,
		{  -0.938963511768773 , -0.033268160205216 , 0.342404370713071 , 0.304902145937230 , 4.613301207800830 , -0.638766224493783  } ,
		{  -0.594098705187880 , -0.679544717007132 , 0.430425029571680 , 0.209981283816519 , 3.587861374149536 , -0.909897785052852  } ,
		{  0.913512431142830 , -0.291152747275836 , 0.284121656867705 , -0.225509137033730 , 1.111213099571701 , 0.446346958366191  } ,
		{  -0.926272155358698 , 0.340304538828062 , -0.161903412750234 , -0.715625681418992 , 6.015421224101938 , -0.305124708836420  } ,
		{  0.907272794350692 , 0.359338817682722 , -0.218475835591651 , -0.949730028579594 , 1.667067519478326 , 0.321233649005808  } ,
		{  0.518703120388430 , -0.575472687723081 , 0.632280205750645 , -0.157775492469517 , 5.809313096268409 , -0.232262797856057  } ,
		{  -0.632193302707533 , -0.683374925011647 , -0.365144272688301 , -0.631799421144978 , 1.405990918974078 , 0.254693004886934  } ,
		{  -0.476721931015739 , 0.613999287901095 , 0.629079545801303 , 0.451550534938906 , 2.347170627474328 , -0.956700370739387  } };

// reference data for tests.
// test 1 -- Y_lm(mu1,eta1,xi1) for l=0..3 --> col's [0 ]-->[15]
// test 2 -- Y_lm(xi2,phi2)     for l=0..3 --> col's [16]-->[31]
// test 3 -- Y_lm(xi3)          for l=0..3 --> col's [32]-->[35]
double ref[10][36] =
{  {  1.000000000000000 , -0.475473748388221 , -0.871625825216203 , 0.119134946215745 , -0.098112952334356 ,
 0.717822819913515 , 0.639597368775740 , -0.179858053852021 , -0.183495324505448 , 0.068975184077738 ,
 0.191223576168849 , -0.814876872305451 , -0.348065423797975 , 0.204175966987980 , 0.357635065782322 ,
 -0.062541553099173 , 1.000000000000000 , 0.079520539634546 , 0.994006543213295 , -0.075016503686760 ,
 -0.010332294229361 , 0.136908114423394 , 0.982073511926267 , -0.129153651592277 , -0.000602788667060 ,
 0.000663801862910 , -0.022965241159335 , 0.191874907484354 , 0.964308132476420 , -0.181007130621179 ,
 -0.001339797996443 , 0.000791320988307 , 1.000000000000000 , -0.824999300846829 , 0.520935769596634 ,
 -0.166286542274327  } ,
 {  1.000000000000000 , -0.676539811544032 , 0.534659021553149 , 0.506392746855438 , -0.593391692716645 ,
 -0.626514150914997 , -0.071209596007744 , 0.468948340381591 , -0.174307100279650 , -0.166656963421008 ,
 -0.709419894694767 , -0.177857114736473 , -0.419894102579925 , 0.133126759641289 , -0.208390050354122 ,
 -0.447052023473318 , 1.000000000000000 , -0.699814905725312 , -0.551657002033746 , 0.453799129386354 ,
 -0.550056719286702 , 0.668671711910394 , -0.043511828160710 , -0.433604140505869 , -0.245784290394346 ,
 -0.070849510624369 , 0.678518371900097 , -0.223541975518558 , 0.407777342482738 , 0.144957120863964 ,
 0.303185381997067 , -0.453217218595486 , 1.000000000000000 , 0.280233096493430 , -0.382204117444556 ,
 -0.365332469868998  } ,
 {  1.000000000000000 , -0.033268160205216 , 0.342404370713071 , -0.938963511768773 , 0.054105090450934 ,
 -0.019730073869780 , -0.324138870374879 , -0.556863359258479 , 0.762574950545748 , -0.069535360844527 ,
 0.041424991625121 , 0.008430065566704 , -0.413247190365974 , 0.237930920138828 , 0.583857464733969 ,
 -0.651999873793364 , 1.000000000000000 , -0.947712072471386 , 0.304902145937230 , -0.094215227510537 ,
 0.154652926113122 , -0.500492439421101 , -0.360552022104308 , -0.049755627702829 , -0.770140511462263 ,
 0.652977305564424 , 0.105439569641543 , 0.310589358572003 , -0.386489906060838 , 0.030876727151850 ,
 -0.525067880272128 , 0.200033215375526 , 1.000000000000000 , -0.638766224493783 , 0.112033434331063 ,
 0.306572193221313  } ,
 {  1.000000000000000 , -0.679544717007132 , 0.430425029571680 , -0.594098705187880 , 0.699257726263576 ,
 -0.506612831970477 , -0.222101440877327 , -0.442911290416326 , -0.094247596921656 , -0.320767009110966 ,
 0.673007329235486 , 0.030657236738147 , -0.446280051810348 , 0.026802393124307 , -0.090709506822378 ,
 0.484888984147825 , 1.000000000000000 , -0.421980287316089 , 0.209981283816519 , -0.881952661747442 ,
 0.644611525211120 , -0.153473492963408 , -0.433861790670150 , -0.320764641608400 , 0.519418771140330 ,
 -0.719069647527491 , 0.302666012368061 , 0.201440046666855 , -0.291825615550765 , 0.421016314459454 ,
 0.243883955004796 , -0.169875613668872 , 1.000000000000000 , -0.909897785052852 , 0.741870968866128 ,
 -0.518446062241937  } ,
 {  1.000000000000000 , -0.291152747275836 , 0.284121656867705 , 0.913512431142830 , -0.460676418097429 ,
 -0.143280094202961 , -0.378912326148126 , 0.449551475692305 , 0.649289590406316 , -0.556738146287687 ,
 -0.292674794565214 , 0.106329931433700 , -0.368843101099805 , -0.333617714674097 , 0.412503636001855 ,
 0.419014062159762 , 1.000000000000000 , 0.873151312224947 , -0.225509137033730 , 0.432148602999194 ,
 0.653556670744346 , -0.341047037522114 , -0.423718443671454 , -0.168794341551909 , -0.498519555359321 ,
 -0.139531775343553 , -0.329558408566336 , -0.398736212789162 , 0.309593392318528 , -0.197346433441118 ,
 0.251380360200900 , -0.717597242726934 , 1.000000000000000 , 0.446346958366191 , -0.201161589135875 ,
 -0.447211077995639  } ,
 {  1.000000000000000 , 0.340304538828062 , -0.161903412750234 , -0.926272155358698 , -0.545967734805173 ,
 -0.095429878794361 , -0.460680927409741 , 0.259749810628100 , 0.642740608476366 , 0.661322276124330 ,
 0.197655081164634 , -0.181080372034234 , 0.232245299061122 , 0.492881191285108 , -0.232689477852637 ,
 -0.373873072169595 , 1.000000000000000 , -0.184802002376616 , -0.715625681418992 , 0.673593426342031 ,
 -0.215608133603983 , 0.229062089230487 , 0.268180173859595 , -0.834919078503187 , 0.363363755348821 ,
 -0.193877874966892 , 0.345013476965823 , -0.176609503789916 , 0.157222754843768 , 0.643732206645552 ,
 -0.581450201069499 , 0.187060601528435 , 1.000000000000000 , -0.305124708836420 , -0.360348368086235 ,
 0.386668457377599  } ,
 {  1.000000000000000 , 0.359338817682722 , -0.218475835591651 , 0.907272794350692 , 0.564680317366578 ,
 -0.135977850264003 , -0.428402463893795 , -0.343322229906568 , 0.601038550176334 , 0.664839855027554 ,
 -0.275861479660301 , -0.167532588314010 , 0.301643200842299 , -0.422992874871265 , -0.293623451509272 ,
 0.312561864239571 , 1.000000000000000 , 0.311620407639781 , -0.949730028579594 , -0.030093094832878 ,
 -0.016242518587766 , -0.512609544755421 , 0.852980690778694 , 0.049502559089119 , -0.083313102318279 ,
 -0.023253812450230 , 0.034493601710203 , 0.669793113165774 , -0.717015607332089 , -0.064681731936523 ,
 0.176928778199010 , 0.006909199966837 , 1.000000000000000 , 0.321233649005808 , -0.345213414119620 ,
 -0.398979373842870  } ,
 {  1.000000000000000 , -0.575472687723081 , 0.632280205750645 , 0.518703120388430 , -0.517016263349553 ,
 -0.630223988477816 , 0.099667387876118 , 0.568053282703123 , -0.053793936306394 , -0.216552161975999 ,
 -0.730968719771661 , -0.352012899078487 , -0.316490609645528 , 0.317287324080359 , -0.076055024843151 ,
 -0.297077249080933 , 1.000000000000000 , -0.450619627502811 , -0.157775492469517 , 0.878663101128771 ,
 -0.685793114614566 , 0.123143154926521 , -0.462660340964002 , -0.240116807583670 , 0.492760484370344 ,
 -0.752779522621071 , 0.241945565786198 , 0.241601144388700 , 0.226844433549195 , -0.471098012177712 ,
 -0.173844285758205 , 0.113139960880454 , 1.000000000000000 , -0.232262797856057 , -0.419080989098114 ,
 0.317070070331058  } ,
 {  1.000000000000000 , -0.683374925011647 , -0.365144272688301 , -0.632193302707533 , 0.748289338181170 ,
 0.432199400057513 , -0.300004490184398 , 0.399829663264154 , -0.058312015893683 , -0.395468958381250 ,
 -0.610968827647367 , 0.139499585948338 , 0.426004384078211 , 0.129051711936182 , 0.047611027139468 ,
 0.500460158861326 , 1.000000000000000 , 0.764629112914148 , -0.631799421144978 , 0.127168436040528 ,
 0.168418884738429 , -0.836740288712135 , 0.098755762838693 , -0.139161525621337 , -0.492323204411783 ,
 -0.324093908512438 , -0.237933182171409 , 0.466295796074212 , 0.317209891102957 , 0.077551463994137 ,
 0.695527861168599 , -0.174710673994139 , 1.000000000000000 , 0.254693004886934 , -0.402697209892496 ,
 -0.340735607336463  } ,
 {  1.000000000000000 , 0.613999287901095 , 0.629079545801303 , -0.476721931015739 , -0.506983267854616 ,
 0.669012233591394 , 0.093611612418360 , -0.519435136416993 , -0.129671132085763 , 0.147950871715564 ,
 -0.713155429709811 , 0.367989540424711 , -0.321237779497753 , -0.285714800915406 , -0.182403794734500 ,
 0.340596194928403 , 1.000000000000000 , 0.636580382515081 , 0.451550534938906 , -0.625193994687522 ,
 -0.689332375029341 , 0.497874908172751 , -0.194153171594582 , -0.488969517824925 , -0.012442232364307 ,
 0.386186377005505 , -0.696017307674258 , 0.007597452232219 , -0.447150304115302 , -0.007461558101653 ,
 -0.012562893294101 , 0.407682931633381 , 1.000000000000000 , -0.956700370739387 , 0.872913399059321 ,
 -0.754060707012793  } } ;

//---------------------------------------------------------------------------//
//              end of testSpherical_Harmonics.cc
//---------------------------------------------------------------------------//
